package com.styytech.dadabao.widge.dialog;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.styytech.dadabao.R;


public class CustomLoadingDialog {
    private Context mContext;
    private boolean mFlag;
    private RelativeLayout ll_loading;
    private LinearLayout ll_loading_dialog;
    private View v_line;
    public ImageView iv_loading;
    private TextView tv_loading_title;
    private TextView tv_loading_content;
    public String title;
    public String message;
    public AlertDialog dlg;

    public CustomLoadingDialog() {
        super();
        // TODO Auto-generated constructor stub
    }

    public CustomLoadingDialog(Context context) {
        super();
        this.mContext = context;
        create();
    }

    /**
     * 显示自定义提示对话框
     *
     * @param context - 上下文
     * @param title   - 提示框标题
     * @param message - 提示框消息内容
     * @param flag    - 设置是否可关闭对话框
     */
    public CustomLoadingDialog(Context context, String title, String message,
                               boolean flag) {
        super();
        this.mContext = context;
        this.title = title;
        this.message = message;
        this.mFlag = flag;
        create();
    }

    private void create() {
        Builder builder = new Builder(new ContextThemeWrapper(
                mContext, R.style.MyCustomDialogStyle1));
        dlg = builder.create();
        dlg.setCancelable(mFlag);
        dlg.show();
        dlg.setContentView(R.layout.custom_loading_dialog);
        // 找自定义布局的控件
        findView();
    }

    public void show() {
        // 初始化用户设置的对话框 目前是固定写法，后期需求变化时再完善
        initView();
        setAnimation();
    }

    /**
     * 找自定义布局的控件
     */
    private void findView() {
        ll_loading = (RelativeLayout) dlg.findViewById(R.id.ll_loading);
        ll_loading_dialog = (LinearLayout) dlg
                .findViewById(R.id.ll_loading_dialog);
        v_line = (View) dlg.findViewById(R.id.v_line);
        iv_loading = (ImageView) dlg.findViewById(R.id.iv_loading);
        tv_loading_title = (TextView) dlg.findViewById(R.id.tv_loading_title);
        tv_loading_content = (TextView) dlg
                .findViewById(R.id.tv_loading_content);
    }

    /**
     * 初始化用户设置的对话框 目前是固定写法，后期需求变化时再完善
     */
    private void initView() {
        if (title != null && !title.isEmpty()) {
            tv_loading_title.setText(title);
        } else {
            tv_loading_title.setVisibility(View.GONE);
            v_line.setVisibility(View.GONE);
        }
        if (message != null && !message.isEmpty()) {
            tv_loading_content.setText(message);
        } else {
            tv_loading_content.setVisibility(View.GONE);
        }
        if (title != null && message != null && title.isEmpty() && message.isEmpty()) {
            ll_loading_dialog.setBackgroundColor(Color.TRANSPARENT);
        }
    }

    /**
     * 设置加载动画
     */
    private void setAnimation() {
        if (iv_loading != null) {
            AnimationDrawable anim = (AnimationDrawable) iv_loading
                    .getBackground();
            if (anim != null) {
                anim.start();
            }
        }
    }

    public void dismiss() {
        dlg.dismiss();
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}
